package com.tanchengjin.admin.modules.blog.validations.annotation;

import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

public class ConfirmPasswordValidator implements ConstraintValidator<ConfirmPassword, String> {
    @Override
    public void initialize(ConfirmPassword constraintAnnotation) {
        ConstraintValidator.super.initialize(constraintAnnotation);
    }

    @Override
    public boolean isValid(String s, ConstraintValidatorContext constraintValidatorContext) {
        ServletRequestAttributes httpServletRequest = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();
        HttpServletRequest request = httpServletRequest.getRequest();
        String password = request.getParameter("password");
        String confPwd = request.getParameter("confPwd");

        if (password == null || confPwd == null) {
            return false;
        }

        if (password.equals(confPwd)) {
            return false;
        }
        return true;
    }
}
